package com.xdl.flytest.util.runner;

import com.xdl.flytest.util.docker.DockerEnvironmentConfig;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 80736
 * @Date: 2019/1/26 20:28
 * @Description: 测试报告工具类
 */
@Component("ReportUtil")
public class ReportUtil {

    private static Logger logger = LoggerFactory.getLogger(ReportUtil.class);

    /**
     * 获取测试报告内容
     *
     * @return html内容
     * @throws IOException
     */
    public static String getReportHtml() throws IOException {
        List<String> reportFileList = getReportFileList();
        if (reportFileList.size() == 1) {
            for (String file : reportFileList) {
                return readFile(file);
            }
        } else {
            throw new RuntimeException("expect 1 file but got more");
        }
        return null;
    }

    /**
     * 清空本地主机测试报告文件夹
     */
    public static void removeReportDirection() {
        try {
            FileUtils.deleteDirectory(new File(DockerEnvironmentConfig.LOCAL_REPORT_SAVED_PATH));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取测试报告文件夹内文件列表
     * filePath为config文件内预先配置
     * @return 测试报告文件路径列表
     * @throws IOException
     */
    public static List<String> getReportFileList() throws IOException {
        return getFileList(DockerEnvironmentConfig.LOCAL_REPORT_SAVED_PATH);
    }

    /**
     * 获取某个文件夹内所有文件名称
     * @param dirPath 文件夹地址
     * @return 文件名称集合
     */
    public static List<String> getFileList(String dirPath)  {
        List<String> list = new ArrayList<>();
        File file = new File(dirPath);
        if (!file.isDirectory()) {
            logger.info(String.format("%s is a file ,path is:%s filename is:%s", dirPath, file.getAbsolutePath(), file.getName()));
        } else {
            logger.info(String.format("%s is a dir ", dirPath));

            String[] fileList = file.list();
            assert fileList != null;
            for (String s : fileList) {
                File readFile = new File(dirPath + "\\" + s);
                if (!readFile.isDirectory()) {
                    logger.info(String.format("path is:%s filename is:%s", file.getAbsolutePath(), file.getName()));
                    list.add(readFile.getAbsolutePath());
                } else {
                    //递归遍历子文件夹
                    getFileList(dirPath + "\\" + s);
                }
            }
        }

        return list;
    }


    /**
     * 读取文件内容
     *
     * @param filePath 文件地址
     * @return 文件内容
     */

    public static String readFile(String filePath) throws IOException {
        File file = new File(filePath);
        InputStream input = null;
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        StringBuffer buffer = new StringBuffer();
        byte[] bytes = new byte[1024];
        try {
            for (int n; (n = input.read(bytes)) != -1; ) {
                buffer.append(new String(bytes, 0, n, "GBK"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
        }


        return buffer.toString();
    }
}
